package com.github.aaronshan.functions.math;

import org.apache.hadoop.hive.ql.exec.Description;
import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.io.BooleanWritable;
import org.apache.hadoop.io.Text;

/**
 * @Auther: GGbond
 * @Date: 2020/3/20 18:19
 * @Description:
 */
@Description(name = "Numeric"
        , value = "_FUNC_() - constant representing is Numeric."
        , extended = "Example:\n > select _FUNC_() from src;")
public class UDFMathIsNumeric extends UDF {

    BooleanWritable result = new BooleanWritable();

    public UDFMathIsNumeric() {
    }

    public BooleanWritable evaluate(Text text) {
        result.set(true);
        for (int i = text.toString().length(); --i >= 0; ) {
            int chr = text.toString().charAt(i);
            if (chr < 48 || chr > 57) {
                result.set(false);
                return result;
            }
        }
        return result;
    }
}